

package top.continew.admin.question.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.user.service.IQuestionService;
import top.continew.admin.question.user.service.question.entity.AbstractQuestion;
import top.continew.admin.question.user.service.question.enums.QuestionTypeEnum;
import top.continew.admin.question.user.service.question.factory.QuestionFactory;
import top.continew.admin.question.user.service.questionset.entity.AbstractQuestionSet;
import top.continew.admin.question.user.service.questionset.entity.WrongQuestionSet;
import top.continew.admin.question.user.service.questionset.enums.QuestionSetTypeEnum;
import top.continew.admin.question.user.service.questionset.factory.QuestionSetFactory;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Service
public class QuestionService implements IQuestionService {

    @Override
    public List<AbstractQuestion> getQuestionList(List<Long> questionIdList) {
        if (questionIdList.isEmpty()) {
            return List.of();
        }
        Executor executor = Executors.newVirtualThreadPerTaskExecutor();
        List<Question> questions = new Question().selectList(Wrappers.lambdaQuery(Question.class)
            .in(Question::getId, questionIdList)
            .ne(Question::getType, QuestionTypeEnum.MATERIAL_QUESTIONS.getCode())
            .eq(Question::getOnlineStatus, 1)
            .orderByAsc(Question::getSort));
        List<CompletableFuture<AbstractQuestion>> completableFutures = questions.stream()
            .map(item -> CompletableFuture.supplyAsync(() -> QuestionFactory.createQuestion(item), executor))
            .toList();
        return completableFutures.stream().map(CompletableFuture::join).toList();
    }

    @Override
    public long getQuestionSum(Long questionSetId, Integer knowledgePointExercise) {
        AbstractQuestionSet abstractQuestionSet = QuestionSetFactory.of(questionSetId, knowledgePointExercise);
        if (abstractQuestionSet == null) {
            return 0L;
        }
        return abstractQuestionSet.getQuestionCount();
    }

    @Override
    public long getQuestionSetId(long questionSetId) {
        AbstractQuestionSet abstractQuestionSet = QuestionSetFactory.of(questionSetId);
        if (abstractQuestionSet == null) {
            return questionSetId;
        }
        return abstractQuestionSet.getQuestionSet().getId();
    }

    @Override
    public void markTheHighlights(Long questionId, Long userId, Long isImportant) {
        AbstractQuestionSet abstractQuestionSet = QuestionSetFactory.of(userId, QuestionSetTypeEnum.WRONG_QUESTION
            .getCode());
        if (abstractQuestionSet instanceof WrongQuestionSet wrongQuestionSet) {
            wrongQuestionSet.markTheHighlights(questionId, isImportant);
        }
    }
}
